1. Technical Field
This invention relates to a method and system for managing memory allocations. More specifically, the invention relates to counting the memory used by an application.
2. Description of the Prior Art
Kernel mode memory is memory that is reserved for operating system applications. There are two classifications of kernel mode memory, paged and non-paged. Both the paged and non-paged kernel mode memory are forms of volatile memory. However, paged memory can be stored temporarily on storage media. The quantity of kernel mode memory in an operating system is statically configured. In order to maintain stability of operating system applications, there needs to be enough kernel mode memory available for each application that utilizes the kernel mode memory. If there is an insufficient amount of kernel mode memory for an operating system application, the operating system will become unstable. Accordingly, limiting the amount of kernel mode memory used by a particular operating system application is critical so that enough kernel mode memory is available for other applications.
Consecutive bytes of memory, kernel mode and non-kernel mode, in a computer system are divided into pages, with each page representing a defined portion of storage space. In one embodiment, each page of memory is 4096 bytes in length. For example, page0 would include bytes 0 through 4095, page1 would include bytes 4096 through 8191, etc. Microsoft Windows operating systems guarantee the following memory allocation format: a memory allocation of less than a page is eight byte aligned, an allocation of one or more pages is page aligned, and an allocation of a page or less does not cross a page boundary.
Prior art computer systems that maintain a count of kernel mode memory used by an application attach a header having a length of eight bytes to each allocation. The header is structured to include a tag to indicate whether the allocation is for paged memory or non-paged memory. In addition, the header stores the size of the kernel mode memory allocated. FIG. 1 is a flow chart (10) demonstrating a prior art tool for allocating kernel mode memory to a calling application. For each calling application, the size and type of tie memory allocation request is obtained (12). A memory allocation is provided from the operating system (14). The size of the memory allocation includes the size provided from the calling application and the size of the header. Following the memory allocation at step (14), a test is conducted to determine if the memory allocation was successful (16). The test consists of checking the value of a pointer returned by the operating system. A negative response to the test at step (16) will result in setting a pointer to the memory allocation to NULL (18), and returning the pointer to the calling application (20). However, a positive response to the test at step (16) will result in setting a pointer to identify the first byte of the allocated memory (22). A tag is an identifying entry in the header pertaining to the type of memory, i.e. paged or non-paged. The tag is stored at the beginning of the header (24), and the size of the allocated memory is stored at another location in the header (26). In one embodiment where the header is eight bytes, the tag information begins at the first byte of the header and the size of the allocated memory begins at the fifth byte of the header. A counter corresponding to the page type, i.e. a paged memory counter or a non-paged memory counter, is incremented to reflect usage of kernel mode memory (28). A pointer is returned to the calling application of the first byte of useful memory (30), i.e. the first byte following the header. Accordingly, the prior art memory allocation process attaches a header to each memory allocation to store the allocation size and type.
Kernel mode memory is allocated based upon availability. When an application does not require use of the kernel mode memory, the memory is released by the application so it may be made available to other applications. FIG. 2 is a flow chart (50) illustrating a prior art process of an application releasing kernel mode memory. A pointer to the first byte of useful memory is obtained from the application in response to a function call made by the application (52). Thereafter, the size of the memory allocation placed in the header at step (26) and the tag information placed in the header at step (24) is extracted from the header (54). A test is conducted to determine if the memory allocation referenced in the tag is for paged memory (56). A negative response to the test at step (56) identifies the memory allocation being release as non-paged memory. A counter for non-paged memory is then decremented by the size of the memory allocation being released by the calling application (58). Conversely, a positive response to the test at step (56) is an indication that the kernel mode memory is paged memory. A counter for paged memory is then decremented by the size of the memory allocation being released by the calling application (60). Following step (58) or step (60), the memory, including the header portion, is released from the calling application (62) and available for assignment in subsequent memory allocation requests.
The process of an application releasing a memory allocation provides data pertaining to usage of kernel mode memory by the application based upon the size of the allocation stored in the associated header. However, there are limitations associated with this prior art approach to tracking use of kernel mode memory. The prior art solution shown in FIGS. 1 and 2 does not have the capability to preserve Windows semantics for alignment of the allocated memory. For example, allocation of a page of memory would require the actual amount of memory allocated to include the header. In effect, the amount of memory allocated may be slightly larger than a page, i.e. by the size of the header. This would cause the memory returned to the calling application to cross a page boundary.
Therefore, there is a need for a solution that tracks the size of the memory allocation while preserving Windows alignment semantics. In addition, such a solution should be efficient and not negatively impact upon system performance.